﻿package com.mui.controls
{
    import __AS3__.vec.*;
    import com.gengine.core.*;
    import com.mui.containers.*;
    import com.mui.containers.globalVariable.*;
    import com.mui.events.*;
    import com.mui.utils.*;
    import fl.controls.*;
    import fl.core.*;
    import flash.events.*;
    import flash.geom.*;

    public class GTabBar extends GBox
    {
        protected var _dataProvider:Array;
        protected var _dataProviderChange:Boolean = false;
        protected var _selectedIndex:int = 0;
        protected var _selectedChange:Boolean = false;
        protected var _buttonStyleName:String = "TabButton";
        protected var _buttonWidth:int = 67;
        protected var _buttonHeight:int = 22;
        protected var _buttonFilters:Array;
        private var _labelName:String = "";
        protected var allButtons:Vector.<Button>;

        public function GTabBar()
        {
            this.allButtons = new Vector.<Button>;
            this.tabChildren = false;
            return;
        }// end function

        override protected function createChildren() : void
        {
            super.createChildren();
            return;
        }// end function

        public function set dataProvider(param1:Array) : void
        {
            if (this._dataProvider == param1)
            {
                return;
            }
            this._dataProvider = param1;
            this._dataProviderChange = true;
            this.invalidate(InvalidationType.ALL);
            return;
        }// end function

        public function getTabRectByLabel(param1:String) : Rectangle
        {
            var _loc_4:Object = null;
            var _loc_2:* = new Rectangle(0, 0, this.buttonWidth - 4, this.buttonHeight - 4);
            if (this._dataProvider == null)
            {
                return _loc_2;
            }
            var _loc_3:int = 0;
            while (_loc_3 < this._dataProvider.length)
            {
                
                _loc_4 = this._dataProvider[_loc_3];
                if (_loc_4["label"] == param1)
                {
                    this.width = direction == GBoxDirection.HORIZONTAL ? (numChildren * (this.buttonWidth + horizontalGap)) : (this.buttonWidth);
                    this.height = direction == GBoxDirection.HORIZONTAL ? (this.buttonHeight) : (numChildren * (this.buttonHeight + verticalGap));
                    if (direction == GBoxDirection.HORIZONTAL)
                    {
                        _loc_2.x = (this.buttonWidth + horizontalGap) * _loc_3 + 2;
                        _loc_2.y = 2;
                    }
                    else
                    {
                        _loc_2.x = 2;
                        _loc_2.y = (this.buttonHeight + verticalGap) * _loc_3 + 2;
                    }
                }
                _loc_3++;
            }
            return _loc_2;
        }// end function

        override public function invalidate(param1:String = "all", param2:Boolean = true) : void
        {
            this._dataProviderChange = true;
            super.invalidate(param1, param2);
            return;
        }// end function

        public function get dataProvider() : Array
        {
            return this._dataProvider;
        }// end function

        public function set selectedIndex(param1:int) : void
        {
            if (this._selectedIndex != param1)
            {
                this._selectedIndex = param1;
                this._selectedChange = true;
                this.invalidate();
            }
            return;
        }// end function

        public function get selectedIndex() : int
        {
            return this._selectedIndex;
        }// end function

        public function get selectedItem() : Object
        {
            if (this._dataProvider && this._dataProvider.length > this._selectedIndex)
            {
                return this._dataProvider[this._selectedIndex];
            }
            return null;
        }// end function

        public function set buttonStyleName(param1:String) : void
        {
            this._buttonStyleName = param1;
            this.invalidate(InvalidationType.STYLES);
            return;
        }// end function

        public function get buttonStyleName() : String
        {
            return this._buttonStyleName;
        }// end function

        public function set buttonWidth(param1:int) : void
        {
            this._buttonWidth = param1;
            return;
        }// end function

        public function get buttonWidth() : int
        {
            return this._buttonWidth;
        }// end function

        public function set buttonHeight(param1:int) : void
        {
            this._buttonHeight = param1;
            return;
        }// end function

        public function get buttonHeight() : int
        {
            return this._buttonHeight;
        }// end function

        public function set buttonFilters(param1:Array) : void
        {
            if (this._buttonFilters == param1)
            {
                return;
            }
            this._buttonFilters = param1;
            return;
        }// end function

        public function get buttonFilters() : Array
        {
            return this._buttonFilters;
        }// end function

        public function set labelName(param1:String) : void
        {
            this._labelName = param1;
            return;
        }// end function

        public function getButtonAt(param1:int) : Button
        {
            if (param1 < this.allButtons.length)
            {
                return this.allButtons[param1];
            }
            return null;
        }// end function

        override protected function updateDisplayList() : void
        {
            var _loc_1:Object = null;
            var _loc_2:GButton = null;
            if (this._dataProviderChange)
            {
                this.reset();
                for each (_loc_1 in this.dataProvider)
                {
                    
                    _loc_2 = UICompomentPool.getUICompoment(GButton);
                    _loc_2.name = _loc_1.name;
                    _loc_2.width = this.buttonWidth;
                    _loc_2.height = this.buttonHeight;
                    _loc_2.styleName = this.buttonStyleName;
                    _loc_2.textField.multiline = true;
                    if (this._labelName != "" && this._labelName != null)
                    {
                        _loc_2.label = _loc_1[this._labelName];
                    }
                    else
                    {
                        _loc_2.label = _loc_1.label;
                    }
                    this.allButtons.push(_loc_2);
                    _loc_2.configEventListener(MouseEvent.CLICK, this.btnClickHandler, false, 0, true);
                    if (this._buttonFilters)
                    {
                        _loc_2.textField.filters = this._buttonFilters;
                    }
                    this.addChild(_loc_2);
                }
                this.checkSelected();
                this._dataProviderChange = false;
            }
            if (this._selectedChange)
            {
                this._selectedChange = false;
                this.checkSelected();
            }
            super.updateDisplayList();
            return;
        }// end function

        protected function checkSelected() : void
        {
            var _loc_1:Button = null;
            var _loc_2:int = 0;
            while (_loc_2 < numChildren)
            {
                
                _loc_1 = getChildAt(_loc_2) as Button;
                if (this.selectedIndex == _loc_2)
                {
                    _loc_1.enabled = false;
                }
                else if (!_loc_1.enabled)
                {
                    _loc_1.enabled = true;
                }
                _loc_1.drawNow();
                _loc_2++;
            }
            return;
        }// end function

        override protected function updateSize() : void
        {
            super.updateSize();
            this.width = direction == GBoxDirection.HORIZONTAL ? (numChildren * (this.buttonWidth + horizontalGap)) : (this.buttonWidth);
            this.height = direction == GBoxDirection.HORIZONTAL ? (this.buttonHeight) : (numChildren * (this.buttonHeight + verticalGap));
            return;
        }// end function

        protected function btnClickHandler(event:MouseEvent) : void
        {
            (event.currentTarget as Button).enabled = false;
            var _loc_2:* = this.getChildIndex(event.currentTarget as Button);
            if (this._selectedIndex != _loc_2 && this._selectedIndex >= 0 && this._selectedIndex < numChildren)
            {
                (this.getChildAt(this._selectedIndex) as Button).enabled = true;
            }
            this._selectedIndex = _loc_2;
            dispatchEvent(new MuiEvent(MuiEvent.GTABBAR_SELECTED_CHANGE, _loc_2));
            return;
        }// end function

        protected function reset() : void
        {
            var _loc_1:Button = null;
            var _loc_2:int = 0;
            while (_loc_2 < this.allButtons.length)
            {
                
                _loc_1 = this.allButtons[_loc_2];
                _loc_1.textField.filters = [];
                _loc_1.removeEventListener(MouseEvent.CLICK, this.btnClickHandler);
                if (_loc_1 is IDispose)
                {
                    (_loc_1 as IDispose).dispose();
                }
                _loc_2++;
            }
            this.allButtons = new Vector.<Button>;
            return;
        }// end function

        override public function dispose(param1:Boolean = true) : void
        {
            this.reset();
            this._dataProvider = [];
            this._dataProviderChange = false;
            this.buttonFilters = [];
            this.buttonStyleName = "TabButton";
            this._selectedIndex = 0;
            super.dispose(param1);
            return;
        }// end function

    }
}
